JBoss Community Archive (Read Only)

Errai

Client Components

The IOC container, by default, provides a set of default injectable bean types. They range from basic services, to injectable proxies for RPC. This section covers the facilities available out-of-the-box.

MessageBus

The type org.jboss.errai.bus.client.framework.MessageBus is globally injectable into any bean. Injecting this type will provide the instance of the active message bus running in the client.

Injecting a MessageBus
@Inject MessageBus bus;

RequestDispatcher

The type org.jboss.errai.bus.client.framework.RequestDispatcher is globally injectable into any bean. Injecting this type will provide a RequestDispatcher instance capable of delivering any messages provided to it, to the the MessageBus.

Injecting a RequestDispatcher
@Inject RequestDispatcher dispatcher;

Caller<?>

The type org.jboss.errai.common.client.api.Caller<?> is a globally injectable RPC proxy. RPC proxies may be provided by various components. For example, JAX-RS or Errai RPC. The proxy itself is agnostic to the underlying RPC mechanism and is qualified by it's type parameterization.

For example:

An example Caller proxy
public void MyClientBean {
  @Inject
  private Caller<MyRpcInterface> rpcCaller;
 
  // ... ///
  
  @EventHandler("button")
  public void onButtonClick(ClickHandler handler) { 
    rpcCaller.call(new RemoteCallback<Void>() {
      public void callback(Void void) {
        // put code here that should execute after RPC response arrives
      }
    ).callSomeMethod();
  }
}

The above code shows the injection of a proxy for the RPC remote interface, MyRpcInterface. For more information on defining RPC proxies see Remote Procedure Calls (RPC) and Creating Requests in Errai JAX-RS.

Sender<?>

The org.jboss.errai.ioc.support.bus.client.Sender<?> interface is the lower-level counterpart to the Caller<?> interface described above. You can inject a Sender to send low-level ErraiBus messages directly to subscribers on any subject.

For example:

  @Inject
  @ToSubject("ListCapitializationService")
  Sender<List<String>> listSender;

  // ... ///

  @EventHandler("button")
  public void onButtonClick(ClickHandler handler) {
    List<String> myListOfStrings = getSelectedCitiesFromForm();
    listSender.send(myListOfStrings, new MessageCallback() {
      public void callback(Message reply) {
        // do stuff with reply
      }
    );
  }

The Sender.send() method is overloaded. The variant demonstrated above takes a value and a MessageCallback to reply receive a reply (assuming the subscriber sends a conversational reply). The following variants are available:

  • send(T)

  • send(T, ErrorCallback)

  • send(T, MessageCallback)

  • send(T, MessageCallback, ErrorCallback)

The reply-to service can also be specified declaratively using the @ReplyTo annotation. This allows the app to receive conversational replies even when using the send() variants that do not take a MessageCallback:

  @Inject
  @ToSubject("ListCapitializationService")
  @ReplyTo("ClientListService")
  Sender<List<String>> listSender;

  // ... ///

  @EventHandler("button")
  public void onButtonClick(ClickHandler handler) {
    List<String> myListOfStrings = getSelectedCitiesFromForm();
    listSender.send(myListOfStrings);
  }

  @Singleton
  @Service
  public static class ClientListService implements MessageCallback {
    @Override
    public void callback(Message message) {
      // do stuff with message
    }
  }

These Sender<?> features are just convenient wrappers around the full-featured programmatic ErraiBus API. See Messaging API Basics and Conversations for full information about low-level ErraiBus communication.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:34:51 UTC, last content change 2013-04-22 20:26:42 UTC.